package core.controls;
import ibm.widgets.WButton;
import ibm.widgets.WComboBox;
import ibm.widgets.WImage;

import com.rational.test.ft.object.interfaces.TestObject;
import com.rational.test.util.regex.Regex;

import core.exceptions.ResultFailureException;
import core.testcases.TestObj;
/**
 * Description   : Functional Test Script
 * @author angel.tsvetkov
 */
public class Image extends Control<WImage>
{
	public Image(String propertyValue, String propertyName, String objClass,
			TestObject parent, String descriptiveName) {
		super(propertyValue, propertyName, objClass, parent, descriptiveName);
		// TODO Auto-generated constructor stub
	}
	
	public Image(TestObject object, String descriptiveName) {
		super(object, descriptiveName);
		this.obj = null;
	}

	public boolean click()
			throws ResultFailureException {
		// Initialize variables
		boolean result = false;
		WImage imageToClick = null;

		// Perform click image
		try {
			// Allocate image
			imageToClick = this.get();

			// Click it
			imageToClick.click();
			
			result = true;
		} catch (Exception e) {
			throw new ResultFailureException(
					"Unexpected assertion on pressing " + this.getDescriptiveName());
		}
		finally
		{
			// Unregister object
			imageToClick.unregister();
		}
		// TODO: Check whether this sleep is needed
		TimeWait(3);

		return result;
	}

	@Override
	public WImage get() {
		if(isCreatedFromTestObject())
		{
			this.obj = new WImage(testObject);
		}
		else
		{
			if(this.isCognosControl())
			{
				TestObject parent = this.getParent();
				this.obj = new WImage(parent.getChildren()[0]);
			}
			else
			{
				Regex toSearch = new Regex(this.PropValue(), Regex.MATCH_MULTILINE);
				this.obj = new WImage(toSearch,this.PropName(),this.getObjClass(),this.getParent());
			}
		}
		return this.obj;
	}

	@Override
	public boolean isEnabled() {
		return this.get().isEnabled();
	}

	@Override
	public boolean isExist() {
		return this.get().ensureObjectIsVisible();
	}
}

